package cn.cds.withyou.admin.stores;

import android.support.annotation.NonNull;

import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cn.cds.withyou.admin.Application;
import cn.cds.withyou.admin.actions.ActionsCreator;
import cn.cds.withyou.admin.actions.RequestActions;
import cn.cds.withyou.lib.model.MemberRequestDto;
import cn.cds.withyou.lib.model.VIPRequestDto;
import cn.ljs.android.buses.ActionBus;
import cn.ljs.android.buses.DataBus;
import cn.ljs.android.store.Store;

/**
 * <p> Description: </p>
 * <p/>
 * <p> Copyright: Copyright (c) 2016 </p>
 *
 * @author: Json.Lee
 * @Version: 1.0
 * @CreateTime: 7/5/2016 11:43
 */
public class RequestStore extends Store<RequestActions> {

    private static RequestStore INSTANCE;

    public static RequestStore get() {
        if (null == INSTANCE) {
            INSTANCE = new RequestStore(Application.getInstance().getActionsBus(), Application.getInstance().getDataBus());
        }
        return INSTANCE;
    }

    private List<MemberRequestDto> mMemberData = new ArrayList<>();
    private List<VIPRequestDto> mVIPData = new ArrayList<>();

    public List<MemberRequestDto> getMemberRequest() {
        return mMemberData;
    }

    public List<VIPRequestDto> getVIPRequest () {
        return mVIPData;
    }


    protected RequestStore(@NonNull ActionBus actionBus, @NonNull DataBus dataBus) {
        super(actionBus, dataBus);
    }

    @Override
    public void onAction(RequestActions actions) {
        RequestEvent event = new RequestEvent();
        switch (actions.getAction()) {
            case RequestActions.LOAD_VIP_REQUEST_SUCC:
                event.code = RequestEvent.LOAD_VIP_REQUEST_SUCC;
                break;
            case RequestActions.LOAD_VIP_REQUEST_FAIL:
                event.code = RequestEvent.LOAD_VIP_REQUEST_FAIL;
                break;
            case RequestActions.LOAD_MEMBER_REQUEST_SUCC:
                event.code = RequestEvent.LOAD_MEMBER_REQUEST_SUCC;
                break;
            case RequestActions.LOAD_MEMBER_REQUEST_FAIL:
                event.code = RequestEvent.LOAD_MEMBER_REQUEST_FAIL;
                break;
            case RequestActions.HANDLE_MEMBER_REQ_FAIL:

                break;
            case RequestActions.HANDLE_VIP_REQ_FAIL:

                break;
            default:
                return;
        }
        mDataBus.dispatch(event);
    }

    @Subscribe
    @Override
    public void onEventMainThread(RequestActions actions) {
        switch (actions.getAction()) {
            case RequestActions.LOAD_MEMBER_REQUEST_SUCC:
                List<MemberRequestDto> data = (List<MemberRequestDto>) actions.getData().get(RequestActions.KEY_DATA_INFO.hashCode(), new ArrayList<>());
                mMemberData.clear();
                mMemberData.addAll(data);
                Collections.sort(mMemberData);
                break;
            case RequestActions.LOAD_VIP_REQUEST_SUCC:
                List<VIPRequestDto> list = (List<VIPRequestDto>) actions.getData().get(RequestActions.KEY_DATA_INFO.hashCode(), new ArrayList<>());
                mVIPData.clear();
                mVIPData.addAll(list);
                Collections.sort(mVIPData);
                break;
            case RequestActions.HANDLE_MEMBER_REQ_SUCC:
                ActionsCreator.get().requestMemberRequest();
                return;
            case RequestActions.HANDLE_VIP_REQ_SUCC:
                ActionsCreator.get().requestVIPRequest();
                return;
            default:
                break;
        }
        super.onEventMainThread(actions);
    }

    public static class RequestEvent extends StoreChangeEvent {

        public final static int LOAD_MEMBER_REQUEST_SUCC = 0x01;
        public final static int LOAD_MEMBER_REQUEST_FAIL = 0x02;
        public final static int LOAD_VIP_REQUEST_SUCC = 0x03;
        public final static int LOAD_VIP_REQUEST_FAIL = 0x04;

        public final static int HANDE_MEMBER_REQ_FAIL = 0x05;
        public final static int HANDE_VIP_REQ_FAIL = 0x06;
    }
}
